package com.hisan.freeride.home.activity;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;


import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.StatusBarUtil;
import com.hisan.freeride.common.view.GradationScrollView;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.RankingBinding;
import com.hisan.freeride.home.adapter.LoveCartAdapter;
import com.hisan.freeride.home.model.Driverinfo;
import com.hisan.freeride.home.model.LoveCart;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 创建时间 : 2017/7/4
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：车主排名页面
 */
public class RankingActivity extends BaseActivity<RankingBinding> {
    private LoveCartAdapter loveCartAdapter;
    private String nickname="";
    private double experience=0.0;
    private String avatar="";
    private Driverinfo.LevelBean levelBean;
    private int page=1;
    private List<LoveCart>loveCarts=new ArrayList<>();
    //图片高度
    private int height;
    @Override
    protected int getLayoutId() {
        return R.layout.ranking;
    }

    @Override
    protected void initView() {
        loveCartAdapter=new LoveCartAdapter(getApplicationContext(),new ArrayList<>(0));
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(this));
        mBinding.rv.setAdapter(loveCartAdapter);
        Bundle bundle=getIntent().getExtras();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
            nickname=bundle.getString("name");
            experience=bundle.getDouble("experience");
            avatar=bundle.getString("avatar");
            levelBean= (Driverinfo.LevelBean) bundle.getSerializable("level");
        }
        init();
    }

    //初始化渐变标题栏
    private void init() {
        LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) mBinding.drawerContent.getLayoutParams();
        params1.setMargins(0,-StatusBarUtil.getStatusBarHeight(this)/4,0,0);
        mBinding.drawerContent.setLayoutParams(params1);
        ViewTreeObserver vto = mBinding.cartHome.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mBinding.titlename.setTextColor(getResources().getColor(R.color.white));
                mBinding.titlename.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                height = mBinding.cartHome.getHeight();
                mBinding.scrollview.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(NestedScrollView v, int x, int y, int oldx, int oldy) {
                        if (y <= 0) {   //设置标题的背景颜色
                            mBinding.detail.setBackgroundColor(Color.argb((int) 0, 255,255,255));
                        } else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                            float scale = (float) y / height;
                            float alpha = (255 * scale);
                            mBinding.titlename.setTextColor(Color.argb((int) alpha, 1,24,28));
                            mBinding.detail.setBackgroundColor(Color.argb((int) alpha, 255,255,255));
                        } else {    //滑动到banner下面设置普通颜色
                            mBinding.detail.setBackgroundColor(Color.argb((int) 255, 255,255,255));
                        }
                    }
                });

            }
        });
    }
    @Override
    protected void initEvent() {
        mBinding.carttopBack.setOnClickListener(v -> finishActivity(true));
        mBinding.pull.setCanRefresh(false);

        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {}
            @Override
            public void loadMore() {
                page++;
                loadData(true);
                mBinding.pull.finishLoadMore();
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<List<LoveCart>>get(AppConfig.Expranking)
                .tag(this)
                .params("page",page)
                .params("size",20)
                .params("city_id", BaseAppLication.getInstance().getCityid())
                .execute(new DialogCallback<List<LoveCart>>(this){
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<List<LoveCart>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            showData();
                            loveCarts.addAll(response.body());
                            if (!CollectionUtils.isNullOrEmpty(avatar)){
                                mBinding.pAvatar.setImageURI(avatar);
                            }
                            if(loveCarts.size()>10){
                                mBinding.pull.setCanLoadMore(true);
                            }else{
                                mBinding.pull.setCanLoadMore(false);
                            }
                            mBinding.cartname.setText(nickname);
                            Double total= experience;
                            int t = (new Double(total)).intValue();
                            mBinding.experience.setText("爱心值 "+t+"分");
                            if (!CollectionUtils.isNullOrEmpty(response.body()))
                                loveCartAdapter.setNewData(loveCarts);
                            switch (levelBean.getId()){
                                case 1:
                                    mBinding.cartHome.setBackgroundResource(R.mipmap.love_main);
                                    break;
                                case 2:
                                    mBinding.cartHome.setBackgroundResource(R.mipmap.love_main2);
                                    break;
                                case 3:
                                    mBinding.cartHome.setBackgroundResource(R.mipmap.love_main3);
                                    break;
                                case 4:
                                    mBinding.cartHome.setBackgroundResource(R.mipmap.love_main4);
                                    break;
                                case 5:
                                    mBinding.cartHome.setBackgroundResource(R.mipmap.love_main5);
                                    break;
                            }
                        }else {
                            if (!CollectionUtils.isNullOrEmpty(loveCarts)) loveCartAdapter.setNewData(loveCarts);else noData();
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<List<LoveCart>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }


}
